From 8c22e70bedf0ad4fd6d7f40c1b6fe408f3319e14 Mon Sep 17 00:00:00 2001 From: nzy Date: Tue, 29 Oct 2024 23:18:36 +0800 Subject: [PATCH 1/2] =?UTF-8?q?SessionChangeCallback=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E6=94=B9&surfaceNode=E4=B8=BB=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E9=94=80=E6=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: nzy --- .../js_scene_session.cpp | 40 ++++++------- .../session/host/include/scene_session.h | 13 ++++- window_scene/session/host/include/session.h | 2 +- .../session/host/src/scene_session.cpp | 58 ++++++++++++++++--- window_scene/session/host/src/session.cpp | 12 ++++ .../test/unittest/scene_session_test2.cpp | 2 +- .../test/unittest/scene_session_test3.cpp | 2 +- 7 files changed, 92 insertions(+), 37 deletions(-) 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 2ce9dfbb6e..3a9b80998f 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 @@ -410,7 +410,7 @@ JsSceneSession::JsSceneSession(napi_env env, const sptr& session) { auto sessionchangeCallback = sptr::MakeSptr(); session->RegisterSessionChangeCallback(sessionchangeCallback); - sessionchangeCallback->clearCallbackFunc_ = [weakThis = wptr(this)](bool needRemove) { + session->RegisterClearCallbackMapCallback([weakThis = wptr(this)](bool needRemove) { if (!needRemove) { TLOGND(WmsLogTag::WMS_LIFE, "clearCallbackFunc needRemove is false"); return; @@ -421,7 +421,7 @@ JsSceneSession::JsSceneSession(napi_env env, const sptr& session) return; } jsSceneSession->ClearCbMap(); - }; + }); sessionchangeCallback_ = sessionchangeCallback; taskScheduler_ = std::make_shared(env); @@ -1536,26 +1536,21 @@ void JsSceneSession::ProcessIsCustomAnimationPlaying() void JsSceneSession::ProcessShowWhenLockedRegister() { - auto sessionchangeCallback = sessionchangeCallback_.promote(); - if (sessionchangeCallback == nullptr) { - WLOGFE("sessionchangeCallback is nullptr"); + auto session = weakSession_.promote(); + if (session == nullptr) { + TLOGE(WmsLogTag::WMS_LIFE, "session is nullptr, id:%{public}d", persistentId_); return; } - sessionchangeCallback->OnShowWhenLocked_ = [weakThis = wptr(this)](bool showWhenLocked) { + const char* const where = __func__; + session->RegisterShowWhenLockedCallback([weakThis = wptr(this), where](bool showWhenLocked) { auto jsSceneSession = weakThis.promote(); if (!jsSceneSession) { - TLOGE(WmsLogTag::WMS_LIFE, "ProcessShowWhenLockedRegister jsSceneSession is null"); + TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s jsSceneSession is null", where); return; } jsSceneSession->OnShowWhenLocked(showWhenLocked); - }; - auto session = weakSession_.promote(); - if (session == nullptr) { - WLOGFE("session is nullptr, id:%{public}d", persistentId_); - return; - } - sessionchangeCallback->OnShowWhenLocked_(session->GetShowWhenLockedFlagValue()); - WLOGFD("success"); + }); + TLOGD(WmsLogTag::WMS_LIFE, "success"); } void JsSceneSession::ProcessRequestedOrientationChange() @@ -1577,20 +1572,21 @@ void JsSceneSession::ProcessRequestedOrientationChange() void JsSceneSession::ProcessForceHideChangeRegister() { - auto sessionchangeCallback = sessionchangeCallback_.promote(); - if (sessionchangeCallback == nullptr) { - WLOGFE("sessionchangeCallback is nullptr"); + auto session = weakSession_.promote(); + if (session == nullptr) { + TLOGE(WmsLogTag::WMS_LIFE, "session is nullptr, id:%{public}d", persistentId_); return; } - sessionchangeCallback->OnForceHideChange_ = [weakThis = wptr(this)](bool hide) { + const char* const where = __func__; + session->RegisterForceHideChangeCallback([weakThis = wptr(this), where](bool hide) { auto jsSceneSession = weakThis.promote(); if (!jsSceneSession) { - TLOGE(WmsLogTag::WMS_LIFE, "ProcessForceHideChangeRegister jsSceneSession is null"); + TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s jsSceneSession is null", where); return; } jsSceneSession->OnForceHideChange(hide); - }; - WLOGFD("success"); + }); + TLOGD(WmsLogTag::WMS_LIFE, "success"); } void JsSceneSession::OnForceHideChange(bool hide) diff --git a/window_scene/session/host/include/scene_session.h b/window_scene/session/host/include/scene_session.h index 84e3f032ec..11551f4509 100644 --- a/window_scene/session/host/include/scene_session.h +++ b/window_scene/session/host/include/scene_session.h @@ -126,11 +126,8 @@ public: NotifySessionEventFunc OnSessionEvent_; NotifyIsCustomAnimationPlayingCallback onIsCustomAnimationPlaying_; NotifyWindowAnimationFlagChangeFunc onWindowAnimationFlagChange_; - NotifyShowWhenLockedFunc OnShowWhenLocked_; NotifyRaiseAboveTargetFunc onRaiseAboveTarget_; - NotifyForceHideChangeFunc OnForceHideChange_; NotifyTouchOutsideFunc OnTouchOutside_; - ClearCallbackMapFunc clearCallbackFunc_; NotifyLandscapeMultiWindowSessionFunc onSetLandscapeMultiWindowFunc_; NotifyLayoutFullScreenChangeFunc onLayoutFullScreenChangeFunc_; NotifyDefaultDensityEnabledFunc onDefaultDensityEnabledFunc_; @@ -401,6 +398,9 @@ public: */ void ClearJsSceneSessionCbMap(bool needRemove); // ONLY Accessed on OS_sceneSession thread void ClearSpecificSessionCbMap(); + void RegisterShowWhenLockedCallback(NotifyShowWhenLockedFunc&& callback); + void RegisterForceHideChangeCallback(NotifyForceHideChangeFunc&& callback); + void RegisterClearCallbackMapCallback(ClearCallbackMapFunc&& callback); void SendPointerEventToUI(std::shared_ptr pointerEvent); bool SendKeyEventToUI(std::shared_ptr keyEvent, bool isPreImeEvent = false); @@ -571,6 +571,13 @@ protected: */ NotifyPrepareClosePiPSessionFunc onPrepareClosePiPSession_; + /* + * Window Lifecycle + */ + NotifyShowWhenLockedFunc onShowWhenLockedFunc_; + NotifyForceHideChangeFunc onForceHideChangeFunc_; + ClearCallbackMapFunc clearCallbackMapFunc_; + private: void NotifyAccessibilityVisibilityChange(); void CalculateCombinedExtWindowFlags(); diff --git a/window_scene/session/host/include/session.h b/window_scene/session/host/include/session.h index 9568609b29..99bb41b4d7 100644 --- a/window_scene/session/host/include/session.h +++ b/window_scene/session/host/include/session.h @@ -127,7 +127,7 @@ public: friend class HidumpController; using Task = std::function; explicit Session(const SessionInfo& info); - virtual ~Session() = default; + virtual ~Session(); bool isKeyboardPanelEnabled_ = false; void SetEventHandler(const std::shared_ptr& handler, const std::shared_ptr& exportHandler = nullptr); diff --git a/window_scene/session/host/src/scene_session.cpp b/window_scene/session/host/src/scene_session.cpp index 2b9115481e..772e8b8d4c 100644 --- a/window_scene/session/host/src/scene_session.cpp +++ b/window_scene/session/host/src/scene_session.cpp @@ -375,14 +375,57 @@ void SceneSession::ClearSpecificSessionCbMap() void SceneSession::ClearJsSceneSessionCbMap(bool needRemove) { - if (sessionChangeCallback_ && sessionChangeCallback_->clearCallbackFunc_) { + if (clearCallbackMapFunc_) { TLOGD(WmsLogTag::WMS_LIFE, "id: %{public}d, needRemove: %{public}d", GetPersistentId(), needRemove); - sessionChangeCallback_->clearCallbackFunc_(needRemove); + clearCallbackMapFunc_(needRemove); } else { TLOGE(WmsLogTag::WMS_LIFE, "get callback failed, id: %{public}d", GetPersistentId()); } } +void SceneSession::RegisterShowWhenLockedCallback(NotifyShowWhenLockedFunc&& callback) +{ + const char* const where = __func__; + auto task = [weakThis = wptr(this), callback = std::move(callback), where] { + auto session = weakThis.promote(); + if (!session) { + TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s session is nullptr", where); + return; + } + session->onShowWhenLockedFunc_ = std::move(callback); + session->onShowWhenLockedFunc_(session->GetShowWhenLockedFlagValue()); + }; + PostTask(task, where); +} + +void SceneSession::RegisterForceHideChangeCallback(NotifyForceHideChangeFunc&& callback) +{ + const char* const where = __func__; + auto task = [weakThis = wptr(this), callback = std::move(callback), where] { + auto session = weakThis.promote(); + if (!session) { + TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s session is nullptr", where); + return; + } + session->onForceHideChangeFunc_ = std::move(callback); + }; + PostTask(task, where); +} + +void SceneSession::RegisterClearCallbackMapCallback(ClearCallbackMapFunc&& callback) +{ + const char* const where = __func__; + auto task = [weakThis = wptr(this), callback = std::move(callback), where] { + auto session = weakThis.promote(); + if (!session) { + TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s session is nullptr", where); + return; + } + session->clearCallbackMapFunc_ = std::move(callback); + }; + PostTask(task, where); +} + WSError SceneSession::Disconnect(bool isFromClient, const std::string& identityToken) { if (isFromClient && SessionHelper::IsMainWindow(GetWindowType())) { @@ -1427,8 +1470,8 @@ WSError SceneSession::OnNeedAvoid(bool status) WSError SceneSession::OnShowWhenLocked(bool showWhenLocked) { WLOGFD("SceneSession ShowWhenLocked status:%{public}d", static_cast(showWhenLocked)); - if (sessionChangeCallback_ != nullptr && sessionChangeCallback_->OnShowWhenLocked_) { - sessionChangeCallback_->OnShowWhenLocked_(showWhenLocked); + if (onShowWhenLockedFunc_) { + onShowWhenLockedFunc_(showWhenLocked); } return WSError::WS_OK; } @@ -3148,8 +3191,8 @@ void SceneSession::NotifyForceHideChange(bool hide) return; } property->SetForceHide(hide); - if (sessionChangeCallback_ && sessionChangeCallback_->OnForceHideChange_) { - sessionChangeCallback_->OnForceHideChange_(hide); + if (onForceHideChangeFunc_) { + onForceHideChangeFunc_(hide); } SetForceTouchable(!hide); if (hide) { @@ -5245,11 +5288,8 @@ void SceneSession::UnregisterSessionChangeListeners() session->sessionChangeCallback_->OnSessionEvent_ = nullptr; session->sessionChangeCallback_->onIsCustomAnimationPlaying_ = nullptr; session->sessionChangeCallback_->onWindowAnimationFlagChange_ = nullptr; - session->sessionChangeCallback_->OnShowWhenLocked_ = nullptr; session->sessionChangeCallback_->onRaiseAboveTarget_ = nullptr; - session->sessionChangeCallback_->OnForceHideChange_ = nullptr; session->sessionChangeCallback_->OnTouchOutside_ = nullptr; - session->sessionChangeCallback_->clearCallbackFunc_ = nullptr; session->sessionChangeCallback_->onSetLandscapeMultiWindowFunc_ = nullptr; session->sessionChangeCallback_->onLayoutFullScreenChangeFunc_ = nullptr; session->sessionChangeCallback_->onRestoreMainWindowFunc_ = nullptr; diff --git a/window_scene/session/host/src/session.cpp b/window_scene/session/host/src/session.cpp index 9484c921d3..20a1b6c134 100644 --- a/window_scene/session/host/src/session.cpp +++ b/window_scene/session/host/src/session.cpp @@ -102,6 +102,18 @@ Session::Session(const SessionInfo& info) : sessionInfo_(info) } } +Session::~Session() +{ + TLOGI(WmsLogTag::WMS_LIFE, "id:%{public}d", GetPersistentId()); + if (mainHandler_) { + mainHandler_->PostTask([surfaceNode = std::move(surfaceNode_)]() mutable { + if (surfaceNode) { + surfaceNode.reset(); + } + }); + } +} + void Session::SetEventHandler(const std::shared_ptr& handler, const std::shared_ptr& exportHandler) { diff --git a/window_scene/test/unittest/scene_session_test2.cpp b/window_scene/test/unittest/scene_session_test2.cpp index fcfbc5ba6c..7f2e4777c5 100644 --- a/window_scene/test/unittest/scene_session_test2.cpp +++ b/window_scene/test/unittest/scene_session_test2.cpp @@ -1086,7 +1086,7 @@ HWTEST_F(SceneSessionTest2, NotifyForceHideChange, Function | SmallTest | Level2 auto func = [sceneSession](bool hide) { sceneSession->SetPrivacyMode(hide); }; - sceneSession->sessionChangeCallback_->OnForceHideChange_ = func; + sceneSession->onForceHideChangeFunc_ = func; EXPECT_NE(nullptr, &func); sceneSession->NotifyForceHideChange(true); diff --git a/window_scene/test/unittest/scene_session_test3.cpp b/window_scene/test/unittest/scene_session_test3.cpp index 01c06ba908..6edc275fb3 100644 --- a/window_scene/test/unittest/scene_session_test3.cpp +++ b/window_scene/test/unittest/scene_session_test3.cpp @@ -309,7 +309,7 @@ HWTEST_F(SceneSessionTest3, ClearSpecificSessionCbMap1, Function | SmallTest | L sptr sessionChangeCallback = new (std::nothrow) MainSession::SessionChangeCallback(); - sessionChangeCallback->clearCallbackFunc_ = [](bool) {}; + sceneSession->clearCallbackMapFunc_ = [](bool) {}; sceneSession->sessionChangeCallback_ = sessionChangeCallback; sceneSession->ClearSpecificSessionCbMap(); } -- Gitee From 72db3d2d8693de87ca5b32b6ebf2feae3800ab37 Mon Sep 17 00:00:00 2001 From: nzy Date: Fri, 1 Nov 2024 10:59:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: nzy --- window_scene/session/host/src/session.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/window_scene/session/host/src/session.cpp b/window_scene/session/host/src/session.cpp index 20a1b6c134..f6b893e77c 100644 --- a/window_scene/session/host/src/session.cpp +++ b/window_scene/session/host/src/session.cpp @@ -107,9 +107,7 @@ Session::~Session() TLOGI(WmsLogTag::WMS_LIFE, "id:%{public}d", GetPersistentId()); if (mainHandler_) { mainHandler_->PostTask([surfaceNode = std::move(surfaceNode_)]() mutable { - if (surfaceNode) { - surfaceNode.reset(); - } + // do nothing }); } } -- Gitee