From 5885a142b18f8cdb9c5a2b268f7489941c0ff95c Mon Sep 17 00:00:00 2001 From: huojia Date: Sat, 19 Jul 2025 17:12:05 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AA=97=E5=8F=A3=E5=A2=9E=E5=8A=A0=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=B8=85=E7=90=86=E5=85=9C=E5=BA=95=20Signed-off-by:?= =?UTF-8?q?=20huojia=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js_scene_session.cpp | 45 ++++++++++++++ .../scene_session_manager/js_scene_session.h | 3 + window_scene/session/host/include/session.h | 3 + window_scene/session/host/src/session.cpp | 21 +++++++ window_scene/test/unittest/session_test2.cpp | 61 +++++++++++++++++++ 5 files changed, 133 insertions(+) diff --git a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.cpp b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.cpp index 2babb501f7..a663dcd794 100644 --- a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.cpp +++ b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.cpp @@ -35,6 +35,7 @@ const std::string SESSION_RECT_CHANGE_CB = "sessionRectChange"; const std::string SESSION_PIP_CONTROL_STATUS_CHANGE_CB = "sessionPiPControlStatusChange"; const std::string SESSION_AUTO_START_PIP_CB = "autoStartPiP"; const std::string CREATE_SUB_SESSION_CB = "createSpecificSession"; +const std::string CLEAR_SUB_SESSION_CB = "clearSubSession"; const std::string BIND_DIALOG_TARGET_CB = "bindDialogTarget"; const std::string RAISE_TO_TOP_CB = "raiseToTop"; const std::string RAISE_TO_TOP_POINT_DOWN_CB = "raiseToTopForPointDown"; @@ -95,6 +96,7 @@ const std::map ListenerFuncMap { {SESSION_PIP_CONTROL_STATUS_CHANGE_CB, ListenerFuncType::SESSION_PIP_CONTROL_STATUS_CHANGE_CB}, {SESSION_AUTO_START_PIP_CB, ListenerFuncType::SESSION_AUTO_START_PIP_CB}, {CREATE_SUB_SESSION_CB, ListenerFuncType::CREATE_SUB_SESSION_CB}, + {CLEAR_SUB_SESSION_CB, ListenerFuncType::CLEAR_SUB_SESSION_CB}, {BIND_DIALOG_TARGET_CB, ListenerFuncType::BIND_DIALOG_TARGET_CB}, {RAISE_TO_TOP_CB, ListenerFuncType::RAISE_TO_TOP_CB}, {RAISE_TO_TOP_POINT_DOWN_CB, ListenerFuncType::RAISE_TO_TOP_POINT_DOWN_CB}, @@ -997,6 +999,24 @@ void JsSceneSession::ProcessCreateSubSessionRegister() WLOGFD("ProcessCreateSubSessionRegister success, id: %{public}d", session->GetPersistentId()); } +void JsSceneSession::ProcessClearSubSessionRegister() +{ + auto session = weakSession_.promote(); + if (session == nullptr) { + TLOGE(WmsLogTag::WMS_LIFE, "session is nullptr, id:%{public}d", persistentId_); + return; + } + session->SetClearSubSessionCallback([weakThis = wptr(this)](const int32_t subPersistentId) { + auto jsSceneSession = weakThis.promote(); + if (!jsSceneSession) { + TLOGNE(WmsLogTag::WMS_LIFE, "ProcessClearSubSessionRegister jsSceneSession is null"); + return; + } + jsSceneSession->OnClearSubSession(subPersistentId); + }); + TLOGI(WmsLogTag::WMS_LIFE, "success"); +} + void JsSceneSession::ProcessBindDialogTargetRegister() { auto sessionchangeCallback = sessionchangeCallback_.promote(); @@ -2161,6 +2181,9 @@ void JsSceneSession::ProcessRegisterCallback(ListenerFuncType listenerFuncType) case static_cast(ListenerFuncType::CREATE_SUB_SESSION_CB): ProcessCreateSubSessionRegister(); break; + case static_cast(ListenerFuncType::CLEAR_SUB_SESSION_CB): + ProcessClearSubSessionRegister(); + break; case static_cast(ListenerFuncType::BIND_DIALOG_TARGET_CB): ProcessBindDialogTargetRegister(); break; @@ -2699,6 +2722,28 @@ void JsSceneSession::OnCreateSubSession(const sptr& sceneSession) taskScheduler_->PostMainThreadTask(task, info); } +void JsSceneSession::OnClearSubSession(int32_t subPersistentId) +{ + auto task = [weakThis = wptr(this), persistentId = persistentId_, subPersistentId, env = env_] { + auto jsSceneSession = weakThis.promote(); + if (!jsSceneSession || jsSceneSessionMap_.find(persistentId) == jsSceneSessionMap_.end()) { + TLOGNE(WmsLogTag::WMS_LIFE, "OnClearSubSession jsSceneSession id:%{public}d has been destroyed", + persistentId); + return; + } + auto jsCallBack = jsSceneSession->GetJSCallback(CLEAR_SUB_SESSION_CB); + if (jsCallBack == nullptr) { + TLOGNE(WmsLogTag::WMS_LIFE, "jsCallBack is nullptr"); + return; + } + napi_value jsSubPersistentId = CreateJsValue(env, subPersistentId); + napi_value argv[] = {jsSubPersistentId}; + napi_call_function(env, NapiGetUndefined(env), jsCallBack->GetNapiValue(), ArraySize(argv), argv, nullptr); + }; + std::string info = "OnClearSubSession id:" + std::to_string(subPersistentId); + taskScheduler_->PostMainThreadTask(task, info); +} + void JsSceneSession::OnBindDialogTarget(const sptr& sceneSession) { if (sceneSession == nullptr) { diff --git a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.h b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.h index 85a6f32e43..8ab008d300 100644 --- a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.h +++ b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.h @@ -39,6 +39,7 @@ enum class ListenerFuncType : uint32_t { SESSION_PIP_CONTROL_STATUS_CHANGE_CB, SESSION_AUTO_START_PIP_CB, CREATE_SUB_SESSION_CB, + CLEAR_SUB_SESSION_CB, BIND_DIALOG_TARGET_CB, RAISE_TO_TOP_CB, RAISE_TO_TOP_POINT_DOWN_CB, @@ -219,6 +220,7 @@ private: void ProcessBufferAvailableChangeRegister(); void ProcessSessionEventRegister(); void ProcessCreateSubSessionRegister(); + void ProcessClearSubSessionRegister(); void ProcessBindDialogTargetRegister(); void ProcessSessionRectChangeRegister(); void ProcessSessionPiPControlStatusChangeRegister(); @@ -275,6 +277,7 @@ private: void OnBufferAvailableChange(const bool isBufferAvailable); void OnSessionEvent(uint32_t eventId, const SessionEventParam& param); void OnCreateSubSession(const sptr& sceneSession); + void OnClearSubSession(int32_t subPersistentId); void OnBindDialogTarget(const sptr& sceneSession); void OnSessionRectChange(const WSRect& rect, const SizeChangeReason& reason = SizeChangeReason::UNDEFINED); void OnSessionPiPControlStatusChange(WsPiPControlType controlType, WsPiPControlStatus status); diff --git a/window_scene/session/host/include/session.h b/window_scene/session/host/include/session.h index 23fae60fe2..1fc324b199 100644 --- a/window_scene/session/host/include/session.h +++ b/window_scene/session/host/include/session.h @@ -82,6 +82,7 @@ using NotifySystemSessionKeyEventFunc = std::function; using NotifyFrameLayoutFinishFunc = std::function; using AcquireRotateAnimationConfigFunc = std::function; +using NotifyClearSubSessionFunc = std::function; class ILifecycleListener { public: @@ -274,6 +275,7 @@ public: void SetNotifyUIRequestFocusFunc(const NotifyUIRequestFocusFunc& func); void SetNotifyUILostFocusFunc(const NotifyUILostFocusFunc& func); void SetGetStateFromManagerListener(const GetStateFromManagerFunc& func); + void SetClearSubSessionCallback(const NotifyClearSubSessionFunc& func); void SetSystemConfig(const SystemSessionConfig& systemConfig); void SetSnapshotScale(const float snapshotScale); @@ -633,6 +635,7 @@ protected: NotifyTerminateSessionFuncTotal terminateSessionFuncTotal_; NotifySessionExceptionFunc sessionExceptionFunc_; NotifySessionExceptionFunc jsSceneSessionExceptionFunc_; + NotifyClearSubSessionFunc clearSubSessionFunc_; AcquireRotateAnimationConfigFunc acquireRotateAnimationConfigFunc_; SystemSessionConfig systemConfig_; diff --git a/window_scene/session/host/src/session.cpp b/window_scene/session/host/src/session.cpp index 752aa44b0d..0f6055973a 100644 --- a/window_scene/session/host/src/session.cpp +++ b/window_scene/session/host/src/session.cpp @@ -2231,6 +2231,19 @@ void Session::SetSessionStateChangeListenser(const NotifySessionStateChangeFunc& PostTask(task, "SetSessionStateChangeListenser"); } +void Session::SetClearSubSessionCallback(const NotifyClearSubSessionFunc& func) +{ + PostTask( + [weakThis = wptr(this), func, where = __func__]() { + auto session = weakThis.promote(); + if (session == nullptr) { + TLOGNE(WmsLogTag::WMS_LIFE, "session is null"); + return; + } + session->clearSubSessionFunc_ = std::move(func); + }, __func__); +} + void Session::SetBufferAvailableChangeListener(const NotifyBufferAvailableChangeFunc& func) { bufferAvailableChangeFunc_ = func; @@ -2335,6 +2348,14 @@ void Session::NotifySessionStateChange(const SessionState& state) if (session->sessionStateChangeFunc_) { session->sessionStateChangeFunc_(state); } + if (!session->sessionStateChangeFunc_ && state == SessionState::STATE_DISCONNECT) { + auto parentSession = session->GetParentSession(); + if (parentSession && parentSession->clearSubSessionFunc_) { + parentSession->clearSubSessionFunc_(session->GetPersistentId()); + TLOGNI(WmsLogTag::WMS_LIFE, "notify clear subSession, parentId: %{public}d, " + "persistentId: %{public}d", parentSession->GetPersistentId(), session->GetPersistentId()); + } + } if (session->sessionStateChangeNotifyManagerFunc_) { session->sessionStateChangeNotifyManagerFunc_(session->GetPersistentId(), state); diff --git a/window_scene/test/unittest/session_test2.cpp b/window_scene/test/unittest/session_test2.cpp index 945cc8238b..c6ddbd46e8 100644 --- a/window_scene/test/unittest/session_test2.cpp +++ b/window_scene/test/unittest/session_test2.cpp @@ -604,6 +604,67 @@ HWTEST_F(WindowSessionTest2, SetSessionStateChangeListenser, Function | SmallTes ASSERT_EQ(WSError::WS_OK, session_->SetFocusable(false)); } +/** + * @tc.name: SetClearSubSessionCallback + * @tc.desc: SetClearSubSessionCallback Test + * @tc.type: FUNC + */ +HWTEST_F(WindowSessionTest2, SetClearSubSessionCallback, TestSize.Level1) +{ + SessionInfo info; + info.abilityName_ = "SetClearSubSessionCallback"; + info.moduleName_ = "SetClearSubSessionCallback"; + info.bundleName_ = "SetClearSubSessionCallback"; + sptr session = sptr::MakeSptr(info); + + NotifyClearSubSessionFunc func = nullptr; + session->SetClearSubSessionCallback(func); + EXPECT_TRUE(session->clearSubSessionFunc_ == nullptr); + + func = [](const int32_t subPersistentId) { + }; + session->SetClearSubSessionCallback(func); + EXPECT_FALSE(session->clearSubSessionFunc_ == nullptr); +} + +/** + * @tc.name: NotifySessionStateChange + * @tc.desc: NotifySessionStateChange Test + * @tc.type: FUNC + */ +HWTEST_F(WindowSessionTest2, NotifySessionStateChange, TestSize.Level1) +{ + g_logMsg.clear(); + LOG_SetCallback(MyLogCallback); + SessionInfo info; + info.abilityName_ = "NotifySessionStateChange"; + info.moduleName_ = "NotifySessionStateChange"; + info.bundleName_ = "NotifySessionStateChange"; + sptr session = sptr::MakeSptr(info); + + session->NotifySessionStateChange(SessionState::STATE_ACTIVE); + EXPECT_TRUE(g_logMsg.find("notify clear subSession") == std::string::npos); + + info.abilityName_ = "parentSession"; + info.moduleName_ = "parentSession"; + info.bundleName_ = "parentSession"; + sptr parentSession = sptr::MakeSptr(info); + EXPECT_NE(parentSession, nullptr); + + session->SetParentSession(parentSession); + session->NotifySessionStateChange(SessionState::STATE_ACTIVE); + EXPECT_TRUE(g_logMsg.find("notify clear subSession") == std::string::npos); + + parentSession->clearSubSessionFunc_ = [](const int32_t subPersistentId) { + }; + session->SetParentSession(parentSession); + session->NotifySessionStateChange(SessionState::STATE_ACTIVE); + EXPECT_TRUE(g_logMsg.find("notify clear subSession") == std::string::npos); + + session->NotifySessionStateChange(SessionState::STATE_DISCONNECT); + EXPECT_TRUE(g_logMsg.find("notify clear subSession") != std::string::npos); +} + /** * @tc.name: SetSessionFocusableChangeListener * @tc.desc: SetSessionFocusableChangeListener Test -- Gitee