!15338 fix abilityStage attch

Merge pull request !15338 from xie/stageAttach
This commit is contained in:
openharmony_ci
2025-07-03 04:10:21 +00:00
committed by Gitee
4 changed files with 78 additions and 8 deletions
@@ -836,6 +836,7 @@ napi_value JsUIExtension::CallObjectMethod(const char *name, napi_value const *a
return handleEscape.Escape(result);
}
TAG_LOGD(AAFwkTag::UI_EXT, "JsUIExtension CallFunction(%{public}s), success", name);
HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
napi_call_function(env, obj, method, argc, argv, nullptr);
return nullptr;
}
@@ -54,6 +54,24 @@ constexpr const char *TASKPOOL_LOWER = "taskpool";
constexpr const char *CALLBACK_SUCCESS = "success";
constexpr const int32_t ARGC_ONE = 1;
namespace {
void *DetachNewBaseContext(napi_env, void *nativeObject, void *)
{
auto *origContext = static_cast<std::weak_ptr<Context> *>(nativeObject);
if (origContext == nullptr) {
TAG_LOGE(AAFwkTag::APPKIT, "origContext is null");
return nullptr;
}
TAG_LOGD(AAFwkTag::APPKIT, "New detached base context");
auto *detachNewContext = new (std::nothrow) std::weak_ptr<Context>(*origContext);
return detachNewContext;
}
void DetachFinalizeBaseContext(void *detachedObject, void *)
{
TAG_LOGD(AAFwkTag::APPKIT, "Finalizer detached base context");
delete static_cast<std::weak_ptr<Context> *>(detachedObject);
}
void RegisterStopPreloadSoCallback(JsRuntime& jsRuntime)
{
std::shared_ptr<StartupManager> startupManager = DelayedSingleton<StartupManager>::GetInstance();
@@ -863,7 +881,6 @@ void JsAbilityStage::SetJsAbilityStage(const std::shared_ptr<Context> &context)
return;
}
}
napi_value contextObj = CreateJsAbilityStageContext(env, context);
shellContextRef_ = JsRuntime::LoadSystemModuleByEngine(env, "application.AbilityStageContext", &contextObj, 1);
if (shellContextRef_ == nullptr) {
@@ -876,20 +893,24 @@ void JsAbilityStage::SetJsAbilityStage(const std::shared_ptr<Context> &context)
return;
}
auto workContext = new (std::nothrow) std::weak_ptr<AbilityRuntime::Context>(context);
napi_coerce_to_native_binding_object(
env, contextObj, DetachCallbackFunc, AttachAbilityStageContext, workContext, nullptr);
auto coerceStatus = napi_coerce_to_native_binding_object(
env, contextObj, DetachNewBaseContext, AttachAbilityStageContext, workContext, nullptr);
if (coerceStatus != napi_ok) {
TAG_LOGW(AAFwkTag::APPKIT, "coerce ability stage context failed: %{public}d", coerceStatus);
delete workContext;
return;
}
napi_add_detached_finalizer(env, contextObj, DetachFinalizeBaseContext, nullptr);
context->Bind(jsRuntime_, shellContextRef_.get());
if (obj != nullptr) {
napi_set_named_property(env, obj, "context", contextObj);
}
TAG_LOGD(AAFwkTag::APPKIT, "Set ability stage context");
napi_status status = napi_wrap(env, contextObj, workContext,
[](napi_env, void* data, void*) {
TAG_LOGD(AAFwkTag::APPKIT, "Finalizer for weak_ptr ability stage context is called");
delete static_cast<std::weak_ptr<AbilityRuntime::Context>*>(data);
},
nullptr, nullptr);
}, nullptr, nullptr);
if (status != napi_ok && workContext != nullptr) {
TAG_LOGD(AAFwkTag::APPKIT, "napi_wrap Failed: %{public}d", status);
delete workContext;
@@ -25,6 +25,26 @@
namespace OHOS {
namespace AbilityRuntime {
namespace {
void* DetachNewAbilityStageContext(napi_env, void* nativeObject, void*)
{
auto* origContext = static_cast<std::weak_ptr<AbilityStageContext> *>(nativeObject);
if (origContext == nullptr) {
TAG_LOGE(AAFwkTag::APPKIT, "origContext is null");
return nullptr;
}
TAG_LOGD(AAFwkTag::APPKIT, "New detached abilityStage context");
auto* detachNewContext = new (std::nothrow) std::weak_ptr<AbilityStageContext>(*origContext);
return detachNewContext;
}
void DetachFinalizeAbilityStageContext(void* detachedObject, void*)
{
TAG_LOGD(AAFwkTag::APPKIT, "Finalizer detached abilityStage context");
delete static_cast<std::weak_ptr<AbilityStageContext> *>(detachedObject);
}
}
void JsAbilityStageContext::ConfigurationUpdated(napi_env env, std::shared_ptr<NativeReference> &jsContext,
const std::shared_ptr<AppExecFwk::Configuration> &config)
{
@@ -96,14 +116,15 @@ napi_value AttachAbilityStageContext(napi_env env, void *value, void *hint)
return nullptr;
}
auto workContext = new (std::nothrow) std::weak_ptr<AbilityStageContext>(ptr);
auto status = napi_coerce_to_native_binding_object(
env, contextObj, DetachCallbackFunc, AttachAbilityStageContext, value, nullptr);
env, contextObj, DetachNewAbilityStageContext, AttachAbilityStageContext, workContext, nullptr);
if (status != napi_ok) {
TAG_LOGW(AAFwkTag::APPKIT, "coerce ability stage context failed: %{public}d", status);
return nullptr;
}
napi_add_detached_finalizer(env, contextObj, DetachFinalizeAbilityStageContext, nullptr);
auto workContext = new (std::nothrow) std::weak_ptr<AbilityStageContext>(ptr);
status = napi_wrap(env, contextObj, workContext,
[](napi_env, void *data, void *) {
TAG_LOGD(AAFwkTag::CONTEXT, "finalizer for weak_ptr ability stage context");
@@ -136,5 +136,32 @@ HWTEST_F(PreloadUIextStateObserverTest, OnAppCacheStateChanged_001, Function | M
EXPECT_TRUE(record == nullptr);
GTEST_LOG_(INFO) << "OnAppCacheStateChanged_001 end";
}
/**
* @tc.number: OnAppCacheStateChanged_002
* @tc.name: OnAppCacheStateChanged
* @tc.desc: Test whether OnAppCacheStateChanged is called normally.
* @tc.type: FUNC
*/
HWTEST_F(PreloadUIextStateObserverTest, OnAppCacheStateChanged_002, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "OnAppCacheStateChanged_002 start";
Want want;
AppExecFwk::AbilityInfo abilityInfo;
AppExecFwk::ApplicationInfo applicationInfo;
std::shared_ptr<AAFwk::AbilityRecord> abilityRecord =
std::make_shared<AAFwk::AbilityRecord>(want, abilityInfo, applicationInfo);
auto extensionRecordSharedPtr = std::make_shared<AbilityRuntime::ExtensionRecord>(abilityRecord);
auto hostPid = extensionRecordSharedPtr->hostPid_ = 10;
std::weak_ptr<AbilityRuntime::ExtensionRecord> extensionRecord = extensionRecordSharedPtr;
PreLoadUIExtStateObserver preLoadUIExtStateObserver(extensionRecord);
AppExecFwk::AppStateData appStateData;
int32_t diedPid = appStateData.pid;
preLoadUIExtStateObserver.OnAppCacheStateChanged(appStateData);
auto record = preLoadUIExtStateObserver.extensionRecord_.lock();
EXPECT_TRUE(record != nullptr);
EXPECT_TRUE(record->hostPid_ != diedPid);
GTEST_LOG_(INFO) << "OnAppCacheStateChanged_002 end";
}
} // namespace AbilityRuntime
} // namespace OHOS